/**
 * Created by maiseries on 2018/1/2.
 */
import Request from '../utils/Request'
import Constants from '../config/Constants'
import {DefaultAssetTypeOptions} from '../config/dict/AssetType'
import {DefaultIndustryOptions} from '../config/dict/Industry'
import {DefaultRegionOptions} from '../config/dict/Region'

export default class SystemService {

    static SYSTEM_MESSAGE_TYPE = {
        "01": "需求消息",
        "02": "认证消息",
        "03": "账号消息",
        "04": "资产消息",
        "05": "其他消息",
    }

    static async splash() {
        let options = {};
        options.url = '/system/splash';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            if(data.detail){
                data.ad = {
                    src: {uri: data.base_url + data.detail.image_name},
                    url: data.detail.jump_link_url,
                    title: data.detail.screen_theme
                }
            }
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async indexAggregate() {
        const options = {};
        options.url = '/system/indexAggr';
        try {
            const res = await Request.post(options);
            const data = Request.valid(res);
            if (data.industry && data.industry.length > 0) {
                storage.save({
                    key: Constants.FIELD_INDUSTRY_STORAGE,
                    data: format.industry(data.industry)
                });
            }
            if (data.assetTypes && data.assetTypes.length > 0) {
                storage.save({
                    key: Constants.FIELD_ASSET_TYPE_STORAGE,
                    data: format.asset(data.assetTypes)
                });
            }
            data.notices = data.notices.length > 0 ?
                data.notices.map((_item, _index) => _index % 3 === 0 ? data.notices.slice(_index, _index + 3) : 0).filter((item, index) => item !== 0) : [];
            data.banners = data.banners && data.banners.banner_text ?
                JSON.parse(data.banners.banner_text).map(item=>{
                    return {
                        src: {uri: data.banners.base_url + item.image_name},
                        url: item.jump_link_url,
                        title: item.banner_title
                    }
                }) : [];
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async aggregateMessage() {
        const options = {};
        options.url = '/system/indexAggr/message';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async setCacheVersion(version) {
        await storage.save({
            key: Constants.CACHE_VERSION_STORAGE,
            data: version
        });
    }

    static async getCacheVersion() {
        try{
            return await storage.load({
                key: Constants.CACHE_VERSION_STORAGE,
                // autoSync(默认为true)意味着在没有找到数据或数据过期时自动调用相应的sync方法
                autoSync: false,
                // syncInBackground(默认为true)意味着如果数据过期，
                // 在调用sync方法的同时先返回已经过期的数据。
                // 设置为false的话，则始终强制返回sync方法提供的最新数据(当然会需要更多等待时间)。
                syncInBackground: false,
            });
        }catch (e) {

        }
    }

    static async getAssetType() {
        try {
            const assetType = await storage.load({
                key: Constants.FIELD_ASSET_TYPE_STORAGE,
                // autoSync(默认为true)意味着在没有找到数据或数据过期时自动调用相应的sync方法
                autoSync: false,
                // syncInBackground(默认为true)意味着如果数据过期，
                // 在调用sync方法的同时先返回已经过期的数据。
                // 设置为false的话，则始终强制返回sync方法提供的最新数据(当然会需要更多等待时间)。
                syncInBackground: false,
            });
            return {
                result: true,
                message: '成功',
                data: {
                    assetTypes: assetType && assetType.length > 0 ? assetType : DefaultAssetTypeOptions
                }
            }
        } catch (err) {
            return {
                result: false,
                message: '缓存读取失败'
            }
        }
    }

    static async getAssetTypeFromWeb() {
        const options = {};
        options.url = '/system/indexAggr';
        try {
            const res = await Request.post(options);
            const data = Request.valid(res);
            if (data.assetTypes && data.assetTypes.length > 0) {
                data.assetTypes = format.asset(data.assetTypes);
                storage.save({
                    key: Constants.FIELD_ASSET_TYPE_STORAGE,
                    data: data.assetTypes
                });
            }
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async getIndustry() {
        try {
            const industry = await storage.load({
                key: Constants.FIELD_INDUSTRY_STORAGE,
                // autoSync(默认为true)意味着在没有找到数据或数据过期时自动调用相应的sync方法
                autoSync: false,
                // syncInBackground(默认为true)意味着如果数据过期，
                // 在调用sync方法的同时先返回已经过期的数据。
                // 设置为false的话，则始终强制返回sync方法提供的最新数据(当然会需要更多等待时间)。
                syncInBackground: false,
            });
            return {
                result: true,
                message: '成功',
                data: {
                    industry: industry && industry.length > 0 ? industry : DefaultIndustryOptions
                }
            }
        } catch (err) {
            return {
                result: false,
                message: '缓存读取失败'
            }
        }
    }

    static getRegion() {
        return DefaultRegionOptions;
    }

    static async feedBackUpload(options) {

        options.url = '/system/feedback/upload';
        try {
            const res = await Request.upload(options);
            const data = Request.valid(res);
            data.id = data.assetServiceFile.id;
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async feedBackSubmit(options) {

        options.url = '/system/feedback/send';
        try {
            const res = await Request.token(options);
            Request.valid(res);
            return {
                result: true,
                message: '成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async messageList(options) {
        options.url = '/message/messageList';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            data.system = {
                content: data.message.content_text,
                date: data.message.create_date,
                isNew: data.count > 0
            };
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async systemMessage(options) {
        options.url = '/message/list';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async businessSendCode(options) {
        options.url = '/system/business/business_sms';
        try {
            const res = await Request.token(options);
            Request.valid(res);
            return {
                result: true,
                message: '成功',
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async businessSubmit(options) {
        options.url = '/system/business/business_save';
        try {
            const res = await Request.token(options);
            Request.valid(res);
            return {
                result: true,
                message: '成功',
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }
}